package com.jic.interaction.hereWeGo.service.impl;

import com.alibaba.fastjson.JSON;
import com.jic.interaction.hereWeGo.common.emuns.OrderStatusEnums;
import com.jic.interaction.hereWeGo.common.exception.OrderStatusException;
import com.jic.interaction.hereWeGo.common.utlis.EncryDecryPtionUtil;
import com.jic.interaction.hereWeGo.common.utlis.HereWeGoHttpUtil;
import com.jic.interaction.hereWeGo.common.utlis.RestResult;
import com.jic.interaction.hereWeGo.model.entity.ChargeInfos;
import com.jic.interaction.hereWeGo.model.entity.Traces;
import com.jic.interaction.hereWeGo.model.vo.request.OrderStatusRequest;
import com.jic.interaction.hereWeGo.model.vo.response.OrderStatusResponse;
import com.jic.interaction.hereWeGo.service.OrderStatusService;
import com.jic.interaction.onlineService.model.vo.response.Trace;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class OrderStatusServiceImpl  implements OrderStatusService {
    @Value("${route.order}")
    private String url;

    @Value("${route.cardpassword}")
    private String cardPassword;

    /**
     *
     *
     * @description: 查询订单状态
     * @return:
     * @author: fengrui
     * @time: 2020/11/13 09:50
     */
    @Override
    public RestResult<OrderStatusResponse> queryOrderStatus(OrderStatusRequest request) throws Exception {
//        log.info("查询订单状态：请求参数{}", request.toString());
//        this.verify(request);
//        String url = this.url+"/mall/order/"+request.getAccount()+"/query";
//        Map<String,String> parem = JSON.parseObject(JSON.toJSONString(request), Map.class);
//        String result = HereWeGoHttpUtil.doRequestPost(url, parem);
//        OrderStatusResponse orderStatusResponse = JSON.parseObject(JSON.toJSONString(result), OrderStatusResponse.class);
//        if(orderStatusResponse==null){
//        }
//        for (int i = 0; i < orderStatusResponse.getChargeInfosList().size(); i++) {
//
//            byte[] encrypt = new byte[0];
//            try {
//                encrypt = EncryDecryPtionUtil.encrypt(orderStatusResponse.getChargeInfosList().get(i)
//                        .getCharge_card_password(), cardPassword);
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//            orderStatusResponse.getChargeInfosList().get(i).setCharge_card_password(EncryDecryPtionUtil.parseByte2HexStr(encrypt));
//            return null;
//        }

        OrderStatusResponse orderStatusResponse = new OrderStatusResponse();
        orderStatusResponse.setOrder_status("08");
        orderStatusResponse.setHwg_order_id("hwg202012041636");

        ChargeInfos chargeInfos = new ChargeInfos();
        chargeInfos.setCharge_card_num("0000001");
        chargeInfos.setCharge_card_password("000002");
        List<ChargeInfos> list = new ArrayList<>();
        list.add(chargeInfos);
        orderStatusResponse.setChargeInfosList(list);
        RestResult restResult = new RestResult();
        restResult.setRet_data(orderStatusResponse);
        List<Traces> tracesList = new ArrayList<>();
        Traces traces = new Traces();
        traces.setAcceptTime("2020-12-04 17:00:00");
        traces.setAcceptStation("已发货");
        tracesList.add(traces);
        orderStatusResponse.setTracesList(tracesList);
        restResult.setStatus("08");
        restResult.setRet_code("0000");
        restResult.setRet_msg("请求成功");
        log.info("查询订单状态结束");
        return restResult;
    }
    /**
     *
     *
     * @description:
     * @return:
     * @author: fengrui
     * @time: 2020/11/10 11:39
     */

    private void verify(OrderStatusRequest request ){
        if (request.getAccount()==null){
            throw new OrderStatusException(OrderStatusEnums.ERROR_ACCOUNT_IS_NULL);
        }
        if (request.getUnionid() ==null || request.getUnionid().equals("")){
            throw new OrderStatusException(OrderStatusEnums.ERROR_UNIONID_IS_NULL);
        }
        if (request.getThird_order()==null || request.getThird_order().equals("")){
            throw new OrderStatusException(OrderStatusEnums.ERROR_THIRD_ORDER_IS_NULL);
        }
    }

}
